/**在app中获取定位*/

// #ifdef APP-PLUS
import permision from "@/js_sdk/wa-permission/permission.js";

function getLocationClass(){
	return new Promise((resolve, reject) => {
		//api获取经纬度
		uni.getLocation({
		  type: 'gcj02',
		  isHighAccuracy: true,
		  success: (res) => {
		    //获取用户坐标
		    let latitude = res.latitude;
		    let longitude = res.longitude;
				resolve({latitude, longitude});
			},fail: (e) => {
				uni.showModal({
					title: '系统提示',
					content: '定位失败，请检查网络或系统定位',
					cancelText: '手动定位',
					confirmText: '重新定位',
					success: function (res) {
						if (res.confirm) {
							//重新调用定位方法
							authorizeAction();
						} else if (res.cancel) {
							resolve(false);
						}
					}
				});
				reject('获取经纬度失败');
			}
		})
	})

}

/**获取当前是ios还是android并且进行判断*/
const authorizeAction = () => {
	return new Promise( async (resolve, reject) => {
		if(uni.getSystemInfoSync().platform == 'ios'){
			//当前是ios系统
			if(permision.judgeIosPermission("location")){
				//已经授权了，调用获取经纬度的方法
				const data = await LocationState('ios');
				resolve(data);
			}else{
				//还没授权
				uni.showModal({
					title: '系统提示',
					content: '是否开启定位权限',
					success: function (res) {
						if (res.confirm) {
							permision.gotoAppPermissionSetting();
						} else if (res.cancel) {}
					}
				});
			}
		}else{
			//当前是安卓系统
			const data = await requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
			resolve(data);
		}
	})
}

/**获取当前安卓设备授权情况*/
async function requestAndroidPermission(permisionID) {
	let result = await permision.requestAndroidPermission(permisionID)
	if (result == 1) {
		 //已获得授权
		 return new Promise( async (resolve, reject) => {
			 const data = await LocationState('android');
			 resolve(data);
		 })
	}else {
		//未获得授权
		uni.showModal({
			title: '系统提示',
			content: '是否开启定位权限',
			success: function (res) {
				if (res.confirm) {
					permision.gotoAppPermissionSetting();
				} else if (res.cancel) {}
			}
		});
	}
}

/**判断当前手机是否开启了定位服务*/
function LocationState(plat){
	return new Promise(async (resolve, reject) => {
		if(permision.checkSystemEnableLocation()){
			//当前已经开启了定位服务，那么可以直接获取当前经纬度
			try{
				const data = await getLocationClass();
				resolve(data)
			}catch(e){
				reject(e)
			}
		}else{
			uni.showModal({
				title: '系统提示',
				content: '是否去开启定位',
				success: function (res) {
					if(plat == 'ios'){   //ios系统的定位服务设置调起
						let UIApplication = plus.ios.import("UIApplication");
						let application2 = UIApplication.sharedApplication();
						let NSURL2 = plus.ios.import("NSURL");
						let setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
						application2.openURL(setting2);
						plus.ios.deleteObject(setting2);
						plus.ios.deleteObject(NSURL2);
						plus.ios.deleteObject(application2);
					}else{  //安卓系统的定位服务设置调起
						let Intent = plus.android.importClass('android.content.Intent');
						let Settings = plus.android.importClass('android.provider.Settings');
						let intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						let main = plus.android.runtimeMainActivity();
						main.startActivity(intent); // 打开系统设置GPS服务页面
					}
				}
			})
		}
	})
}

export default {
	authorizeAction    //入口
}
			
// #endif